# #write_csv(canada_word2vec, "data/word2vec/canada_word2vec.csv")
#
# UN_word2vec<-model %>%
#   closest_to(model[[as.character('onu')]],100)
# #write_csv(UN_word2vec, "data/word2vec/UN_word2vec.csv")
#
#europe<-subset(read.csv('data/word2vec/europe_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#europeans<-subset(read.csv('data/word2vec/europeans_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#norway<-subset(read.csv('data/word2vec/norway_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#latam<-subset(read.csv('data/word2vec/latam_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#panama<-subset(read.csv('data/word2vec/panama_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#duque<-subset(read.csv('data/word2vec/duque_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#colombia<-subset(read.csv('data/word2vec/colombia_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#chile<-subset(read.csv('data/word2vec/chile_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#general<-subset(read.csv('data/word2vec/general_foreign_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#canada<-subset(read.csv('data/word2vec/canada_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#un<-subset(read.csv('data/word2vec/UN_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#foreign_actors<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(europe, europeans, norway, latam, panama,
#                                                           duque, colombia, chile, general,
#                                                           canada, un))))
#write.csv(foreign_actors, 'foreign_actors_terms.csv', row.names=F)
colombia_specific_terms<-read.csv('ReplicationData/colombia_terms.csv', col.names = 'term')$term
foreign_actors<-read.csv('ReplicationData/foreign_actors_terms.csv', col.names = 'term')$term
##################################
######Cuba/Russia
# cuba_word2vec<-model %>%
#   closest_to(model[[as.character('cuba')]],100)
# #write_csv(cuba_word2vec, "data/word2vec/cuba_word2vec.csv")
#
# castro_word2vec<-model %>%
#   closest_to(model[[as.character('castro')]],100)
# #write_csv(castro_word2vec, "data/word2vec/castro_word2vec.csv")
#
# ruso_word2vec<-model %>%
#   closest_to(model[[as.character('rusos')]],100)
# #write_csv(ruso_word2vec, "data/word2vec/ruso_word2vec.csv")
#
# putin_word2vec<-model %>%
#   closest_to(model[[as.character('putin')]],100)
# #write_csv(putin_word2vec, "data/word2vec/putin_word2vec.csv")
#
# #cuba<-subset(read.csv('data/word2vec/cuba_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #castro<-subset(read.csv('data/word2vec/castro_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #ruso<-subset(read.csv('data/word2vec/ruso_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #putin<-subset(read.csv('data/word2vec/putin_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#
# #cuba_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(cuba, castro, ruso, putin))))
#
# #write.csv(cuba_terms, 'cuba_terms.csv', row.names=F)
cuba_terms<-read.csv('ReplicationData/cuba_terms.csv', col.names = 'term')$term
##########################################
#####protest
# protest_word2vec<-model %>%
#   closest_to(model[[as.character("manifestacion")]],100)
# #write_csv(protest_word2vec, "data/word2vec/protest_word2vec.csv")
#
# mobilization_word2vec<-model %>%
#   closest_to(model[[as.character("movilizacion")]],100)
# #write_csv(mobilization_word2vec, "data/word2vec/mobilization_word2vec.csv")
#
# protesta_word2vec<-model %>%
#   closest_to(model[[as.character("protesta")]],100)
# #write_csv(protesta_word2vec, "data/word2vec/protesta_word2vec.csv")
#
# protest_englishword2vec<-model %>%
#   closest_to(model[[as.character('protest', 'protests')]],100)
# #write_csv(protest_englishword2vec, "data/word2vec/protest_englishword2vec.csv")
#
# #protest<-subset(read.csv('data/word2vec/protest_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #mobilization<-subset(read.csv('data/word2vec/mobilization_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #protesta<-subset(read.csv('data/word2vec/protesta_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
# #protest_english<-subset(read.csv('data/word2vec/protest_englishword2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#
# #protest_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(protest, protesta, mobilization, protest_english))))
#
# #write.csv(protest_terms, 'protest_terms.csv', row.names=F)
protest_terms<-read.csv('ReplicationData/protest_terms.csv', col.names = 'term')$term
#############################################
##########exile
# exile_word2vec<-model %>%
#   closest_to(model[[as.character('exilio')]],100)
# #write_csv(exile_word2vec, "data/word2vec/exile_word2vec.csv")
#
# exile_englishword2vec<-model %>%
#   closest_to(model[[as.character("exile", 'exiled', 'expelled')]],100)
#write_csv(exile_englishword2vec, "data/word2vec/exile_englishword2vec.csv")
#exile<-subset(read.csv('data/word2vec/exile_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#exile_english<-subset(read.csv('data/word2vec/exile_englishword2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#exile_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(exile, exile_english, 'exiled'))))
#write.csv(exile_terms, 'exile_terms.csv', row.names=F)
exile_terms<-read.csv('ReplicationData/exile_terms.csv', col.names = 'term')$term
#############################################
#########political prisoners
# presos_word2vec<-model %>%
#   closest_to(model[[as.character("preso_politico")]],100)
# #write_csv(presos_word2vec, "data/word2vec/presos_word2vec.csv")
#
# encarcelado_word2vec<-model %>%
#   closest_to(model[[as.character("encarcelado")]],100)
# #write_csv(encarcelado_word2vec, "data/word2vec/encarcelado_word2vec.csv")
#
# encerrado_word2vec<-model %>%
#   closest_to(model[[as.character("encerrado")]],100)
# #write_csv(encerrado_word2vec, "data/word2vec/encerrado_word2vec.csv")
#
# presos_english_word2vec<-model %>%
#   closest_to(model[[as.character("prisoners")]],100)
#write_csv(presos_english_word2vec, "data/word2vec/presos_english_word2vec.csv")
#presos<-subset(read.csv('data/word2vec/presos_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#encarcelado<-subset(read.csv('data/word2vec/encarcelado_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#encerrado<-subset(read.csv('data/word2vec/encerrado_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#presos_english<-subset(read.csv('data/word2vec/presos_english_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#presos_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(presos, presos_english,
#                                                         encarcelado,
#                                                         encerrado))))
#write.csv(presos_terms, 'presos_terms.csv', row.names=F)
presos_terms<-read.csv('ReplicationData/presos_terms.csv', col.names = 'term')$term
################################################
############other repression
# torture_word2vec<-model %>%
#   closest_to(model[[as.character("tortura")]],100)
# #write_csv(torture_word2vec, "data/word2vec/torture_word2vec.csv")
#
# execucion_word2vec<-model %>%
#   closest_to(model[[as.character("ejecuciones")]],100)
# #write_csv(execucion_word2vec, "data/word2vec/execucion_word2vec.csv")
#
# disappearances_word2vec<-desapariciones_word2vec<-model %>%
#   closest_to(model[[as.character("desapariciones")]],100)
# #write_csv(disappearances_word2vec, "data/word2vec/disappearances_word2vec.csv")
#
# repression_word2vec<-model %>%
#   closest_to(model[[as.character("represion")]],100)
# #write_csv(repression_word2vec, "data/word2vec/repression_word2vec.csv")
#
# repression_english_word2vec<-model %>%
#   closest_to(model[[as.character('torture', 'repression')]],100)
# #write_csv(repression_english_word2vec, "data/word2vec/repression_english_word2vec.csv")
#
# human_rights_word2vec<-model %>%
#   closest_to(model[[as.character("derechos_humanos")]],100)
# #write_csv(human_rights_word2vec, "data/word2vec/human_rights_word2vec.csv")
#
# human_rights_english_word2vec<-model %>%
#   closest_to(model[[as.character("human_rights")]],100)
# #write_csv(human_rights_english_word2vec, "data/word2vec/human_rights_english_word2vec.csv")
#torture<-subset(read.csv('data/word2vec/torture_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#repression<-subset(read.csv('data/word2vec/repression_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#ejecucion<-subset(read.csv('data/word2vec/execucion_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#disappearances<-subset(read.csv('data/word2vec/disappearances_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#repression_english<-subset(read.csv('data/word2vec/repression_english_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#humanrights<-subset(read.csv('data/word2vec/human_rights_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#humanrights_english<-subset(read.csv('data/word2vec/human_rights_english_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#repression_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(torture, repression, repression_english,
#                                                             humanrights, humanrights_english, ejecucion, disappearances))))
#write.csv(repression_terms, 'repression_terms.csv', row.names=F)
repression_terms<-read.csv('ReplicationData/repression_terms.csv', col.names = 'term')$term
#################################################
##########criticisms of the regime
# narco_word2vec<-model %>%
#   closest_to(model[[as.character('narco')]], 100)
# #write_csv(narco_word2vec, "data/word2vec/narco_word2vec.csv")
#
# fascist_word2vec<-model %>%
#   closest_to(model[[as.character("fascista")]],100)
# #write_csv(fascist_word2vec, "data/word2vec/fascist_word2vec.csv")
#
# dictator_word2vec<-model %>%
#   closest_to(model[[as.character("dictador")]],100)
# #write_csv(dictator_word2vec, "data/word2vec/dictator_word2vec.csv")
#narco<-subset(read.csv('data/word2vec/narco_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#fascist<-subset(read.csv('data/word2vec/fascist_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#dictator<-subset(read.csv('data/word2vec/dictator_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#narco_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(narco))))
#dictator_terms<-gsub('\\.', "\\\\.",gsub('_', ' ',unique(c(fascist, dictator))))
#write.csv(narco_terms, 'narco_terms.csv', row.names=F)
#write.csv(dictator_terms, 'dictator_terms.csv', row.names=F)
dictator_terms=read.csv('ReplicationData/dictator_terms.csv', col.names = 'term')$term
narco_terms=read.csv('ReplicationData/narco_terms.csv', col.names = 'term')$term
##################################################
#########services
#
# hospital_word2vec<-model %>%
#   closest_to(model[[as.character('hospital')]],100)
# #write_csv(hospital_word2vec, "data/word2vec/hospital_word2vec.csv")
#
# agua_word2vec<-model %>%
#   closest_to(model[[as.character('agua')]],100)
# #write_csv(agua_word2vec, "data/word2vec/agua_word2vec.csv")
#
# luz_word2vec<-model %>%
#   closest_to(model[[as.character(c(' luz ', 'electricidad'))]],100)
# #write_csv(luz_word2vec, "data/word2vec/luz_word2vec.csv")
#
# comida_word2vec<-model %>%
#   closest_to(model[[as.character(c('comida'))]],100)
# #write_csv(comida_word2vec, "data/word2vec/comida_word2vec.csv")
#hospital<-subset(read.csv('data/word2vec/hospital_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#agua<-subset(read.csv('data/word2vec/agua_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#luz<-subset(read.csv('data/word2vec/luz_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#comida<-subset(read.csv('data/word2vec/comida_word2vec.csv', stringsAsFactors=F), keep_terms==1)$word
#services_terms<-gsub('\\.', "\\\\.", gsub('_', ' ',unique(c(hospital, agua, luz, comida))))
#write.csv(services_terms, 'services_terms.csv', row.names=F)
services_terms=read.csv('ReplicationData/services_terms.csv', col.names = 'term')$term
########Dictionary analysis data
data$tweeted_exile<-NA
data$tweeted_exile[date(data$object.postedTime)<data$date_of_exile]<-0
data$tweeted_exile[date(data$object.postedTime)>=data$date_of_exile]<-1
data$tweeted_exile[is.na(data$tweeted_exile)]<-0
data$days_since_exile<-as.numeric(date(data$object.postedTime)-data$date_of_exile)
#foreign actors
data$us_terms<-str_detect(tolower(data$text_nstops), paste(us_terms, collapse="|"))
data$foreign_actors_terms<-str_detect(tolower(data$text_nstops), paste(foreign_actors, collapse="|"))
data$foreign_actors_terms<-ifelse(data$foreign_actors_terms==T&str_detect(tolower(data$text_nstops), c(' copa |futbol|soccer'))==FALSE, TRUE, FALSE)
data$colombia_specific_terms<-str_detect(tolower(data$text_nstops), paste(colombia_specific_terms, collapse='|'))
data$all_foreign_actors<-I((data$us_terms+data$foreign_actors_terms)>0)
#foreign policies
data$military_terms<-str_detect(tolower(data$text_nstops), paste(intervention_terms, collapse="|"))
data$sanctions_terms<-str_detect(tolower(data$text_nstops), paste(sanctions_terms, collapse="|"))
data$diplomacy_terms<-str_detect(tolower(data$text_nstops), paste(diplomacy_terms, collapse="|"))
data$aggressive_foreign_policy<-I((data$military_terms+data$sanctions_terms)>0)
data$all_foreign_policy<-I((data$military_terms+data$sanctions_terms+data$diplomacy_terms)>0)
#protest
data$protest_terms<-str_detect(tolower(data$text_nstops), paste(protest_terms, collapse="|"))
#repression
data$exile_terms<-str_detect(tolower(data$text_nstops), paste(exile_terms, collapse="|"))
data$presos_terms<-str_detect(tolower(data$text_nstops), paste(presos_terms, collapse="|"))
data$repression_terms<-str_detect(tolower(data$text_nstops), paste(repression_terms, collapse="|"))
data$all_repression<-I((data$presos_terms+data$repression_terms)>0)
#criticism
data$narco_terms<-str_detect(tolower(data$text_nstops), paste(narco_terms, collapse="|"))
data$dictator_terms<-str_detect(tolower(data$text_nstops), paste(dictator_terms, collapse="|"))
data$cuba_terms<-str_detect(tolower(data$text_nstops), paste(cuba_terms, collapse="|"))
data$harsh_criticism_terms<-I((data$narco_terms+data$dictator_terms+data$cuba_terms+data$all_repression)>0)
#services
data$service_terms<-str_detect(tolower(data$text_nstops), paste(services_terms, collapse="|"))
## t-test analysis/data production
#function to perform pre/post t-test
ttest_terms<-function(days_since, term){
t.test(term[data$days_since_exile<days_since&data$days_since_exile>=0],
term[data$days_since_exile<0&data$days_since_exile>-days_since])
}
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-data.frame(cbind(conf.low, conf.high, est, label))
for (i in 2:length(periods)){
ttest_result<-ttest_terms(periods[i], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-rbind(tt_df, data.frame(cbind(conf.low, conf.high, est, label)))
}
tt_df$period<-periods
tt_df
}
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
tt_foreign_policy
data$all_foreign_policy[data$days_since_exile<days_since&data$days_since_exile>=0],
t.test(data$all_foreign_policy[data$days_since_exile<days_since&data$days_since_exile>=0],
data$all_foreign_policy[data$days_since_exile<0&data$days_since_exile>-days_since])
days_since<-30
t.test(data$all_foreign_policy[data$days_since_exile<days_since&data$days_since_exile>=0], data$all_foreign_policy[data$days_since_exile<0&data$days_since_exile>-days_since])
xx<-t.test(data$all_foreign_policy[data$days_since_exile<days_since&data$days_since_exile>=0], data$all_foreign_policy[data$days_since_exile<0&data$days_since_exile>-days_since])
stargazer(xx)
t.test(data$all_foreign_policy[data$days_since_exile<days_since&data$days_since_exile>=0], data$all_foreign_policy[data$days_since_exile<0&data$days_since_exile>-days_since])
xx$statistic
xx$p.value
xx$parameter
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
stat<-round(ttest_results$statistic, 2)
p.value<-round(ttest_results$p.value, 2)
degfree<-ttest_results$parameter
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree))
for (i in 2:length(periods)){
ttest_result<-ttest_terms(periods[i], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tstat<-round(ttest_results$statistic, 2)
p.value<-round(ttest_results$p.value, 2)
degfree<-ttest_results$parameter
tt_df<-rbind(tt_df, data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree)))
}
tt_df$period<-periods
tt_df
}
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
tt_military<-ttest_multiple_periods(c(120, 60, 30), data$military_terms, 'prepost', 'Military Intervention')
is.numeric(xx$statistic)
is.numeric(xx$parameter)
round(xx$parameter, 2)
round(xx$statistic, 2)
ttest_result
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
stat<-round(ttest_result$statistic$, 2)
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
stat<-round(ttest_result$statistic$, 2)
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
stat<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-ttest_result$parameter
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree))
for (i in 2:length(periods)){
ttest_result<-ttest_terms(periods[i], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tstat<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-ttest_result$parameter
tt_df<-rbind(tt_df, data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree)))
}
tt_df$period<-periods
tt_df
}
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
tt_foreign_policy
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
stat<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-round(ttest_result$parameter, 2)
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree))
for (i in 2:length(periods)){
ttest_result<-ttest_terms(periods[i], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tstat<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-ttest_result$parameter
tt_df<-rbind(tt_df, data.frame(cbind(conf.low, conf.high, est, label, tstat, p.value, degfree)))
}
tt_df$period<-periods
tt_df
}
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
tt_military<-ttest_multiple_periods(c(120, 60, 30), data$military_terms, 'prepost', 'Military Intervention')
tt_sanctions<-ttest_multiple_periods(c(120, 60, 30), data$sanctions_terms, 'prepost', 'Sanctions')
tt_diplomacy<-ttest_multiple_periods(c(120, 60, 30), data$diplomacy_terms, 'prepost', 'Diplomacy')
tt_protest<-ttest_multiple_periods(c(120, 60, 30), data$protest_terms, 'prepost', 'Protest')
tt_criticism<-ttest_multiple_periods(c(120, 60, 30), data$harsh_criticism_terms, 'prepost', 'Criticism')
tt_narco<-ttest_multiple_periods(c(120, 60, 30), data$narco_terms, 'prepost', 'Narco-State')
tt_dictator<-ttest_multiple_periods(c(120, 60, 30), data$dictator_terms, 'prepost', 'Dictator')
tt_cuba<-ttest_multiple_periods(c(120, 60, 30), data$cuba_terms, 'prepost', 'Cuban/Russian Influence')
tt_repression<-ttest_multiple_periods(c(120, 60, 30), data$all_repression, 'prepost', 'Repression')
tt_services<-ttest_multiple_periods(c(120, 60, 30), data$service_terms, 'prepost', 'Services')
ttest_results<-rbind(tt_services, tt_repression, tt_cuba, tt_dictator, tt_narco,
tt_criticism,tt_protest, tt_diplomacy, tt_sanctions, tt_military,
tt_foreign_policy)
ttest_results
head(ttest_results)
ttest_result$statistic
xx$statistic
xx$statistic$t
round(xx$statistic, 2)
#plotting for pre/post t-test
ttest_multiple_periods<-function(periods, term, type, label){
divisor<-ifelse(type=='percent of mean', mean(term), 1)
ttest_result<-ttest_terms(periods[1], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
tval<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-round(ttest_result$parameter, 2)
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tt_df<-data.frame(cbind(conf.low, conf.high, est, label, tval, p.value, degfree))
for (i in 2:length(periods)){
ttest_result<-ttest_terms(periods[i], term)
conf.low<-ttest_result$conf.int[1]/divisor*100
conf.high<-ttest_result$conf.int[2]/divisor*100
est<-(ttest_result$estimate[1]/divisor-ttest_result$estimate[2]/divisor)*100
tval<-round(ttest_result$statistic, 2)
p.value<-round(ttest_result$p.value, 2)
degfree<-ttest_result$parameter
tt_df<-rbind(tt_df, data.frame(cbind(conf.low, conf.high, est, label, tval, p.value, degfree)))
}
tt_df$period<-periods
tt_df
}
tt_foreign_policy<-ttest_multiple_periods(c(120, 60, 30), data$all_foreign_policy, 'prepost', 'Foreign Policy')
tt_military<-ttest_multiple_periods(c(120, 60, 30), data$military_terms, 'prepost', 'Military Intervention')
tt_sanctions<-ttest_multiple_periods(c(120, 60, 30), data$sanctions_terms, 'prepost', 'Sanctions')
tt_diplomacy<-ttest_multiple_periods(c(120, 60, 30), data$diplomacy_terms, 'prepost', 'Diplomacy')
tt_protest<-ttest_multiple_periods(c(120, 60, 30), data$protest_terms, 'prepost', 'Protest')
tt_criticism<-ttest_multiple_periods(c(120, 60, 30), data$harsh_criticism_terms, 'prepost', 'Criticism')
tt_narco<-ttest_multiple_periods(c(120, 60, 30), data$narco_terms, 'prepost', 'Narco-State')
tt_dictator<-ttest_multiple_periods(c(120, 60, 30), data$dictator_terms, 'prepost', 'Dictator')
tt_cuba<-ttest_multiple_periods(c(120, 60, 30), data$cuba_terms, 'prepost', 'Cuban/Russian Influence')
tt_repression<-ttest_multiple_periods(c(120, 60, 30), data$all_repression, 'prepost', 'Repression')
tt_services<-ttest_multiple_periods(c(120, 60, 30), data$service_terms, 'prepost', 'Services')
ttest_results<-rbind(tt_services, tt_repression, tt_cuba, tt_dictator, tt_narco,
tt_criticism,tt_protest, tt_diplomacy, tt_sanctions, tt_military,
tt_foreign_policy)
head(ttest_results)
xtable(ttest_results)
library(xtable)
install.package('xtable')
install.packages('xtable')
library(xtable)
xtable(ttest_results)
round_df <- function(df, digits) {
nums <- vapply(df, is.numeric, FUN.VALUE = logical(1))
df[,nums] <- round(df[,nums], digits = digits)
(df)
}
round_df(ttest_results, digits=3)
ttest_results_table<-round_df(ttest_results, digits=2)
ttest_results_table<-ttest_results_table[,c('label', 'est', 'conf.low', 'conf.high',
'tval', 'p.value', 'period')]
xtable(ttest_results)
xtable(ttest_results_table)
colnames(ttest_results_table)
rownames(ttest_results_table) <- NULL
xtable(ttest_results_table)
ttest_results_table<-round_df(ttest_results, digits=2)
ttest_results_table<-ttest_results_table[,c('label', 'est', 'conf.low', 'conf.high',
'tval', 'p.value', 'period')]
rownames(ttest_results_table) <- NULL
xtable(ttest_results_table)
round_df <- function(df, digits) {
nums <- vapply(df, is.numeric, FUN.VALUE = logical(1))
df[,nums] <- round(df[,nums], digits = digits)
(df)
}
ttest_results_table<-round_df(ttest_results, digits=2)
ttest_results_table
ttest_results_table %>%
mutate_if(tt_services, round)
ttest_results_table %>%
mutate_if(is.numeric, round)
ttest_results_table %>%
mutate_if(is.numeric, round(2))
round(ttest_results, digits=2)
ttest_results %>% mutate(across(where(is.numeric), round, digits=3))
is.numeric(tt_services$conf.low)
ttest_results<-rbind(tt_services, tt_repression, tt_cuba, tt_dictator, tt_narco,
tt_criticism,tt_protest, tt_diplomacy, tt_sanctions, tt_military,
tt_foreign_policy)
ttest_results$period<-factor(ttest_results$period, levels=c(120, 60, 30))
ttest_results$est<-as.numeric(as.character(ttest_results$est))
ttest_results$conf.low<-as.numeric(as.character(ttest_results$conf.low))
ttest_results$conf.high<-as.numeric(as.character(ttest_results$conf.high))
library(xtable)
round_df <- function(x, digits) {
# round all numeric variables
# x: data frame
# digits: number of digits to round
numeric_columns <- sapply(x, mode) == 'numeric'
x[numeric_columns] <-  round(x[numeric_columns], digits)
x
}
round_df(ttest_results, 3)
ttest_results<-rbind(tt_services, tt_repression, tt_cuba, tt_dictator, tt_narco,
tt_criticism,tt_protest, tt_diplomacy, tt_sanctions, tt_military,
tt_foreign_policy)
ttest_results$est<-as.numeric(as.character(ttest_results$est))
ttest_results$conf.low<-as.numeric(as.character(ttest_results$conf.low))
ttest_results$conf.high<-as.numeric(as.character(ttest_results$conf.high))
library(xtable)
round_df <- function(x, digits) {
# round all numeric variables
# x: data frame
# digits: number of digits to round
numeric_columns <- sapply(x, mode) == 'numeric'
x[numeric_columns] <-  round(x[numeric_columns], digits)
x
}
round_df(ttest_results, 3)
ttest_results_table<-round_df(ttest_results, 2)
ttest_results_table<-ttest_results_table[,c('label', 'est', 'conf.low', 'conf.high',
'tval', 'p.value', 'period')]
rownames(ttest_results_table) <- NULL
xtable(ttest_results_table)
?xtable
ttest_results_table<-ttest_results_table[,c('label','period', 'est', 'conf.low', 'conf.high',
'tval', 'p.value')]
rownames(ttest_results_table) <- NULL
xtable(ttest_results_table)
